package offer.secnod;

import listNodePractice.ListNode;

public class Offer18 {

    ListNode  remove( ListNode  head,int target){
        if (head==null){
            return null;
        }
        ListNode pre=null;
      ListNode head1=head;

        while (head!=null){
          if (head.val==target){
              if ( pre==null){
                  return head.next;
              }else{
                  pre.next=head.next;
                  return  head1;
              }

          }else {

              pre=head;
              head=head.next;

          }
        }
 return  null;
    }


    public ListNode deleteNode(ListNode head, int val) {
        ListNode cur=head;
        ListNode previous=null;
        while(cur.val!=val){
            previous=cur;
            cur=cur.next;

        }
        if( previous != null){
            previous.next=cur.next;
        }

        return previous == null ? head.next : head;

    }

}
